package com.captainbank.tcl.tclapplication.ui.activity.function;

import android.annotation.TargetApi;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Handler;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.Toolbar;
import android.transition.Transition;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.ui.views.video.EmptyVideoPlayer;
import com.captainbank.tcl.tclapplication.utils.MyToast;
import com.captainbank.tcl.tclapplication.utils.SharePreferenceManager;
import com.captainbank.tcl.tclapplication.utils.StringUtil;
import com.seu.magicfilter.utils.MagicFilterType;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.listener.LockClickListener;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by TCL on 2019/5/28.
 * Describe:
 */
public class VideoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_url)
    EditText etUrl;
    @BindView(R.id.video_player)
//    StandardGSYVideoPlayer videoPlayer;
    EmptyVideoPlayer videoPlayer;

    private String url;
    private OrientationUtils orientationUtils;
    private boolean isPlay = false, isPause = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_video;
    }

    @Override
    protected void initData() {
        SharePreferenceManager.init(this,SharePreferenceManager.Live);
        String videoUrl = SharePreferenceManager.getLiveVideoUrl();
        if(StringUtil.checkIsNotNull(videoUrl)){
            url = videoUrl;
        }else{
            url = "rtmp://ossrs.net/zux/saw5a";
        }
        etUrl.setText(url);
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_video));
        toolbar.inflateMenu(R.menu.menu_video);

        initEmptyVideoPlayer(url);
//        initVideoPlayer();
    }

    //初始化无UI播放器
    private void initEmptyVideoPlayer(String mUrl) {
        videoPlayer.setUp(mUrl, true, "");
        videoPlayer.startPlayLogic();
    }

    //初始化普通播放器
    private void initVideoPlayer() {
        //外部辅助的旋转，帮助全屏
        orientationUtils = new OrientationUtils(this, videoPlayer);
        //初始化不打开外部的旋转
        orientationUtils.setEnable(false);

        GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.icon);

        gsyVideoOption
                .setThumbImageView(imageView)
                .setIsTouchWiget(true)
                .setRotateViewAuto(false)
                .setLockLand(false)
                .setAutoFullWithSize(true)
                .setShowFullAnimation(false)
                .setNeedLockFull(true)
                .setUrl(url)
                .setCacheWithPlay(false)
                .setVideoTitle("直播测试")
                .setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        //开始播放了才能旋转和全屏
                        orientationUtils.setEnable(true);
                        isPlay = true;
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
                        Debuger.printfError("***** onQuitFullscreen **** " + objects[0]);//title
                        Debuger.printfError("***** onQuitFullscreen **** " + objects[1]);//当前非全屏player
                        if (orientationUtils != null) {
                            orientationUtils.backToProtVideo();
                        }
                    }
                }).setLockClickListener(new LockClickListener() {
            @Override
            public void onClick(View view, boolean lock) {
                if (orientationUtils != null) {
                    //配合下方的onConfigurationChanged
                    orientationUtils.setEnable(!lock);
                }
            }
        }).build(videoPlayer);

        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //直接横屏
                orientationUtils.resolveByClick();

                //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                videoPlayer.startWindowFullscreen(VideoActivity.this, true, true);
            }
        });
    }

    @Override
    protected void setListener() {
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.toolbar_save:
                            String newUrl = etUrl.getText().toString().trim();
                            if(StringUtil.checkIsNotNull(newUrl)){
                                videoPlayer.release();
                                initEmptyVideoPlayer(newUrl);
                                SharePreferenceManager.setLiveVideoUrl(newUrl);
                                MyToast.toastSuccess(VideoActivity.this,"成功存入视频地址",false);
                            }else{
                                MyToast.toastError(VideoActivity.this,"请输入视频地址",false);
                            }
                            break;
                        case R.id.toolbar_clear:
                            etUrl.setText("");
                            SharePreferenceManager.setLiveVideoUrl("");
                            MyToast.toastSuccess(VideoActivity.this,"已经清空视频地址",false);
                            break;
                        case R.id.toolbar_test1:
                            videoPlayer.release();
                            initEmptyVideoPlayer("https://res.exexm.com/cw_145225549855002");
                            break;
                        case R.id.toolbar_test2:
                            videoPlayer.release();
                            initEmptyVideoPlayer("http://9890.vod.myqcloud.com/9890_4e292f9a3dd011e6b4078980237cc3d3.f20.mp4");
                            break;
                    }
                    return true;
            }
        });
    }

    @Override
    public void onBackPressed() {
        //普通
//        if (orientationUtils != null) {
//            orientationUtils.backToProtVideo();
//        }
//        if (GSYVideoManager.backFromWindowFull(this)) {
//            return;
//        }
//        super.onBackPressed();
        //无UI
        videoPlayer.setVideoAllCallBack(null);
        GSYVideoManager.releaseAllVideos();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            super.onBackPressed();
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    finish();
//                    overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
                }
            }, 500);
        }
    }


    @Override
    protected void onPause() {
        //普通
//        videoPlayer.getCurrentPlayer().onVideoPause();
//        super.onPause();
//        isPause = true;
        //无UI
        super.onPause();
    }

    @Override
    protected void onResume() {
        //普通
//        videoPlayer.getCurrentPlayer().onVideoResume(false);
//        super.onResume();
//        isPause = false;
        //无UI
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //普通
//        if (isPlay) {
//            videoPlayer.getCurrentPlayer().release();
//        }
//        if (orientationUtils != null)
//            orientationUtils.releaseListener();
        //无UI
        videoPlayer.release();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
        if (isPlay && !isPause) {
            videoPlayer.onConfigurationChanged(this, newConfig, orientationUtils, true, true);
        }
    }
}
